//
// Created by YueChuan on 2023/5/4.
//

#include "stream.h"

namespace yuechuan {
    int Stream::readFixSize(void *buffer, size_t length) {
        size_t offset = 0;
        size_t left = length;
        while (left > 0) {
            size_t len = read((char *) buffer + offset, left);
            if (len <= 0) {
                return len;
            }
            offset += len;
            left -= len;
        }
        return length;
    }

    int Stream::readFixSize(ByteArray::ptr ba, size_t length) {
        int64_t left = length;
        while (left > 0) {
            int64_t len = read(ba, left);
            if (len <= 0) {
                return len;
            }
            left -= len;
        }
        return length;
    }

    int Stream::writeFixSize(const void *buffer, size_t length) {

        size_t offset = 0;
        int64_t left = length;
        while (left > 0) {
            int64_t len = write((const char *) buffer + offset, left);
            if (len <= 0) {
                return len;
            }
            left -= len;
            offset += len;
        }
        return length;
    }

    int Stream::writeFixSize(ByteArray::ptr ba, size_t length) {
        int64_t left = length;
        while (left > 0) {
            int64_t len = write(ba, left);
            if (len <= 0) {
                return len;
            }
            left -= len;
        }
        return left;
    }
}